In [10]:
import os
os.chdir('/Users/Tony/Documents/Git Folder/seelviz/graphfiles/LukeGraphs/')
In [11]:
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
from plotly import tools
import plotly
plotly.offline.init_notebook_mode()
In [12]:
import numpy as np
import nibabel as nb
import networkx as nx
import pandas as pd
import re
In [13]:
import plotly.plotly as py
from plotly.graph_objs import *
In [14]:
# Change the filename below to run different graphml files
graphMLfilename = 'Control258localeq.graphml'
G = nx.read_graphml(graphMLfilename)
In [15]:
def get_brain_figure(g, plot_title=''):
    """
    Returns the plotly figure object for vizualizing a 3d brain network.

    g: networkX object of brain
    """

    # grab the node positions from the graphML file
    V = nx.number_of_nodes(g)
    attributes = nx.get_node_attributes(g,'attr')
    node_positions_3d = pd.DataFrame(columns=['x', 'y', 'z'], index=range(V))
    for n in g.nodes_iter():
        node_positions_3d.loc[n] = [int((re.findall('\d+', str(attributes[n])))[0]), int((re.findall('\d+', str(attributes[n])))[1]), int((re.findall('\d+', str(attributes[n])))[2])]

    # grab edge endpoints
    edge_x = []
    edge_y = []
    edge_z = []

    for e in g.edges_iter():
        #strippedSource = int(e[0].replace('s', ''))
        #strippedTarget = int(e[1].replace('s', ''))
        source_pos = node_positions_3d.loc[e[0]]
        target_pos = node_positions_3d.loc[e[1]]
    
        edge_x += [source_pos['x'], target_pos['x'], None]
        edge_y += [source_pos['y'], target_pos['y'], None]
        edge_z += [source_pos['z'], target_pos['z'], None]

    # node style
    node_trace = Scatter3d(x=node_positions_3d['x'],
                           y=node_positions_3d['y'],
                           z=node_positions_3d['z'],
                           mode='markers',
                           # name='regions',
                           marker=Marker(symbol='dot',
                                         size=6,
                                         opacity=0.5,
                                         color='purple'),
                           # text=[str(r) for r in range(V)],
                           # text=atlas_data['nodes'],
                           hoverinfo='text')

    # edge style
    edge_trace = Scatter3d(x=edge_x,
                           y=edge_y,
                           z=edge_z,
                           mode='lines',
                           line=Line(color='cyan', width=1),
                           hoverinfo='none')
    
    # axis style
    axis = dict(showbackground=False,
                showline=False,
                zeroline=False,
                showgrid=False,
                showticklabels=False)
    
    # overall layout
    layout = Layout(title=plot_title,
                    width=800,
                    height=900,
                    showlegend=False,
                    scene=Scene(xaxis=XAxis(axis),
                                yaxis=YAxis(axis),
                                zaxis=ZAxis(axis)),
                    margin=Margin(t=50),
                    hovermode='closest',
                    paper_bgcolor='rgba(1,1,1,1)',
                    plot_bgcolor='rgb(1,1,1)')

    data = Data([node_trace, edge_trace])
    fig = Figure(data=data, layout=layout)

    return fig
In [16]:
output = get_brain_figure(G, '')
In [17]:
outputlocation = '../../reveal/html/graphmlhtmls/'
plotly.offline.plot(output, filename = outputlocation + graphMLfilename + '.html')
Out[17]:
'file:///Users/Tony/Documents/Git Folder/seelviz/reveal/html/graphmlhtmls/Control258localeq.graphml.html'
In [18]:
iplot(output, validate=False)
In [ ]:
## Testing and debugging code (kept for my sake) -- ignore
# print nx.get_node_attributes(G, 'attr')
# attributes = nx.get_node_attributes(G,'attr')
# print attributes['s2040']
# print str(attributes['s2040'])
# print (re.findall('\d+', str(attributes['s2040'])))
# print type(re.findall('\d+', str(attributes['s2040'])))
# print int((re.findall('\d+', str(attributes['s2040'])))[0])
# print [int((re.findall('\d+', str(attributes['s2040'])))[0]), int((re.findall('\d+', str(attributes['s2040'])))[1]), int((re.findall('\d+', str(attributes['s2040'])))[2])]
# print nx.number_of_nodes(G)